<template>
  <div class="article-page">
    <nav class="my-nav van-hairline--bottom">
      <a
        href="javascript:;"
        @click="changeStatus('weight_desc')"
        :class="{ active: queryObj.sorter === 'weight_desc' }"
        >推荐</a
      >
      <a
        href="javascript:;"
        @click="changeStatus(null)"
        :class="{ active: queryObj.sorter === null }"
        >最新</a
      >
      <div class="logo"><img src="@/assets/logo.png" alt="" /></div>
    </nav>

    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <ArticleItem v-for="item in list" :key="item.id" :info="item" />
    </van-list>
  </div>
</template>

<script>
import { getArticleList } from '@/api/article'

export default {
  name: 'article-page',
  data () {
    return {
      // 渲染的数据
      list: [],
      // 请求时的参数
      queryObj: {
        current: 1,
        sorter: 'weight_desc'
      },
      loading: false,
      finished: false
    }
  },
  methods: {
    async onLoad () {
      console.log('我是load事件，我被触发了')
      // console.log(this.loading)
      const data = await getArticleList(this.queryObj)
      // console.log(data)

      this.list = [...this.list, ...data.data.rows]

      // 获取数据成功以后将loading修改为false，以便于下次加载获取数据
      this.loading = false

      // 每次请求完之后让当前页自增
      this.queryObj.current += 1

      if (this.queryObj.current > data.data.pageTotal) {
        // 如果当前页大于总页数时，将finished变量改为true，不再触发load事件
        this.finished = true
      }
    },

    // 修改列表的排序
    changeStatus (type) {
      this.queryObj.sorter = type
      this.queryObj.current = 1 // 还原请求对象中的参数

      // 将数据源还原
      this.list = []

      // 为了避免重复重发load事件
      this.loading = true

      // 重新调用onLoad发起请求获取最新的排序和数据
      this.onLoad()
    }
  },
  async created () {}
}
</script>

<style lang="less" scoped>
.article-page {
  margin-bottom: 50px;
  margin-top: 44px;
  .my-nav {
    height: 44px;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 999;
    background: #fff;
    display: flex;
    align-items: center;
    > a {
      color: #999;
      font-size: 14px;
      line-height: 44px;
      margin-left: 20px;
      position: relative;
      transition: all 0.3s;
      &::after {
        content: '';
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        bottom: 0;
        width: 0;
        height: 2px;
        background: #222;
        transition: all 0.3s;
      }
      &.active {
        color: #222;
        &::after {
          width: 14px;
        }
      }
    }
    .logo {
      flex: 1;
      display: flex;
      justify-content: flex-end;
      > img {
        width: 64px;
        height: 28px;
        display: block;
        margin-right: 10px;
      }
    }
  }
}
</style>
